<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>人事管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <!--引入本地的css样式-->
    <link rel="stylesheet" href="../../static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../static/css/admin.css" media="all">

</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" id="human_name" name="human_name" placeholder="输入姓名"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" id="btn_search" lay-submit
                            lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layuiadmin-btn-admin" lay-event="batchdel">删除</button>
                    <button class="layui-btn layuiadmin-btn-admin" lay-event="add" id="add_human"
                            data-type="add_human">登记档案
                    </button>
                </div>
            </script>

            <!--<script type="text/html" id="toolbarDemo">-->
            <!--<div style="padding-bottom: 10px;">-->
            <!--<button class="layui-btn layuiadmin-btn-admin" lay-event="batchdel">删除</button>-->
            <!--<button class="layui-btn layuiadmin-btn-admin" id="add_employee" data-type="add_employee">添加</button>-->
            <!--</div>-->
            <!--</script>-->
            <!--数据表格-->
            <table class="layui-hide" id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>

            <script type="text/html" id="humanSexTpl">
                {{#  if(d.human_sex == 1){ }}
                <span>男</span>
                {{#  } else if(d.human_sex == 2){ }}
                <span>女</span>
                {{#  } }}
            </script>
            <script type="text/html" id="deptIdTpl">
                {{#  if(d.dept_id == 1){ }}
                <span>平台研发中心</span>
                {{#  } else if(d.dept_id == 2){ }}
                <span>产品设计中心</span>
                {{#  } else if(d.dept_id == 3){ }}
                <span>系统维护中心</span>
                {{#  } }}
            </script>
            <script type="text/html" id="fileStatusTpl">
                {{#  if(d.file_status == 1){ }}
                <span>起草</span>
                {{#  } else if(d.file_status == 2){ }}
                <span>审核中</span>
                {{#  } else if(d.file_status == 3){ }}
                <span>审核通过</span>
                {{#  } else if(d.file_status == 4){ }}
                <span>删除</span>
                {{#  } }}
            </script>


            <script type="text/html" id="buttonTpl">
                {{#  if(d.check == true){ }}
                <button class="layui-btn layui-btn-xs">已审核</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button>
                {{#  } }}
            </script>

            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                {{#  if(d.role == '超级管理员'){ }}
                <a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
                {{#  } else { }}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
                {{#  } }}
            </script>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="toCheck">提交审核</a>
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>


        </div>
    </div>
</div>

<script src="../../static/js/jquery-3.6.0.min.js"></script>
<!--引入本地layui.js-->
<script src="../../static/layui/layui.js"></script>
<!--js-->
<script type="text/javascript">
    layui.config({
        base: '../../static/layui/layuiadmin/' //静态资源所在路径js
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('index');
</script>

<script type="text/javascript">
    layui.use(['table', 'layedit', 'form'], function () {
        var table = layui.table;
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        var layedit = layui.layedit;
        table.render({
            elem: '#LAY-user-back-manage'
            , url: '/hrms/queryHumanList'
            , title: '档案数据表'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'human_id', title: 'ID', fixed: 'left', unresize: true, sort: true}
                , {field: 'human_name', title: '姓名',}
                , {field: 'human_sex', title: '性别', templet: '#humanSexTpl', sort: true}
                , {field: 'human_birthday', title: '出生日期'}
                , {field: 'human_education', title: '学历'}
                , {field: 'human_email', title: 'email', sort: true}
                , {field: 'human_tel', title: '联系电话', sort: true}
                , {field: 'dept_id', title: '所在部门', templet: '#deptIdTpl'}
                , {field: 'human_resume', title: '个人履历'}
                , {field: 'file_status', title: '档案状态', templet: '#fileStatusTpl'}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 190}
            ]]
            , page: true
            , limits: [5, 10, 15, 20]
        });

        //自定义验证规则
        form.verify({
            name: function (value) {
                if (value.length < 3) {
                    return '用户名称至少3字符';
                }
            }
            , email: [
                /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
                , '请填写正确的邮箱地址'
            ]
            , tel: [
                /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
                , '请填写正确的手机号码'
            ]
            , content: function (value) {

                layedit.sync(editIndex);
            }
        });

        //搜索点击事件
        $("#btn_search").click(function () {
            var human_name = $("#human_name").val();
            //执行重载
            table.reload('LAY-user-back-manage', {
                url: '/hrms/search',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    human_name: human_name
                }
            });
        });
        //监听行工具事件
        table.on('tool(LAY-user-back-manage)', function (obj) {
            var data = obj.data;//选中的当前行的数据
            if (obj.event === 'del') {
                layer.confirm('真的删除行吗？', function (index) {
                    //通过ajax实现真正的删除
                    $.ajax({
                        url: "/hrms/del",
                        type: "post",
                        data: {human_id: data.human_id},
                        dataType: "json",
                        success: function (res) {
                            layer.alert(res.msg);
                            obj.del();
                            layer.close(index);
                        }
                    })

                });
            } else if (obj.event === 'toCheck') {
                layer.confirm('确认提交审核吗?', function (index) {
                    //通过ajax实现修改档案状态
                    if (data.file_status !== 1) {
                        layer.msg('当前状态不可提交审核！');
                    } else {
                        $.ajax({
                            url: "/hrms/toCheck",
                            type: "post",
                            data: {human_id: data.human_id},
                            dataType: "json",
                            success: function (res) {
                                layer.alert(res.msg);
                                obj.del();
                                //刷新
                                window.location.reload();
                                layer.close(index);
                            }
                        })

                    }
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '编辑员工'
                    , content: 'files_change.html'
                    , area: ['450px', '550px']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var submitData = body.find('#myform').serialize();
                        //提交 Ajax 成功后，静态表单中的数据
                        $.ajax({
                            url: "/hrms/updateHuman",
                            type: "post",
                            data: submitData,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.msg(res.msg, {
                                        offset: '15px'
                                        , icon: 1
                                        , time: 1000
                                    }, function () {
                                        //刷新
                                        window.location.reload();
                                    });
                                }
                                if (res.code == 500) {
                                    layer.msg(res.msg, {
                                        offset: '15px'
                                        , icon: 1
                                        , time: 1000
                                    }, function () {
                                        //刷新
                                        window.location.reload();
                                    });
                                }
                                layer.close(index); //关闭弹层

                            }
                        })
                    }
                    , success: function (layero, index) {//点击弹出框时，执行的函数。
                        //获取弹出编辑框的body内容
                        var body = layer.getChildFrame('body', index);
                        body.find("input[name=human_id]").val(data.human_id);
                        //找到body中input输入框，向employee_name输入框中塞入选中行的值
                        body.find("input[name=human_name]").val(data.human_name);
                        // body.find("radio[name=human_sex]").val(data.human_sex);//单选框不能锁定
                        body.find("input[name=human_birthday]").val(data.human_birthday);
                        body.find("select[name=human_education]").val(data.human_education);
                        body.find("input[name=human_email]").val(data.human_email);
                        body.find("input[name=human_tel]").val(data.human_tel);
                        body.find("select[name=dept_id]").val(data.dept_id);
                        body.find("textarea[name=human_resume]").val(data.human_resume);

                    }
                });
            }
        });

        //头工具栏事件
        table.on('toolbar(LAY-user-back-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'batchdel':
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg('未选中删除的数据');
                    } else {
                        //删除，通过ajax方式进行删除
                        layer.confirm('真的删除选中行的数据么', function (index) {
                            var ids = new Array();
                            $.each(data, function (index, item) {
                                //循环遍历勾选数据，只将要删除数据的id号获取处理，塞给数组对象
                                ids.push(item.human_id);
                            })
                            //通过ajax传递给后端
                            $.ajax({
                                url: "/hrms/batchdel",
                                type: "post",
                                data: {ids: ids},
                                traditional: true,//单纯的传递数组，需要将其进行传统序列化操作
                                dataType: "json",
                                success: function (res) {
                                    layer.msg(res.msg, {
                                        offset: '15px'
                                        , icon: 1
                                        , time: 2000
                                    }, function () {
                                        //刷新
                                        window.location.reload();
                                    });
                                }
                            })
                        });

                    }
                    break;
                case 'add':
                    layer.open({
                        type: 2
                        , title: '注册档案'
                        , content: 'files_register.html'
                        , area: ['450px', '550px']
                        , maxmin: true
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            var submitData = body.find('#myform').serialize();
                            //提交 Ajax 成功后，静态表单中的数据
                            $.ajax({
                                url: "/hrms/addHuman",
                                type: "post",
                                data: submitData,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 200) {
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        }, function () {
                                            //刷新
                                            window.location.reload();
                                        });
                                    }
                                    if (res.code == 500) {
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        }, function () {
                                            //刷新
                                            window.location.reload();
                                        });
                                    }
                                    layer.close(index); //关闭弹层

                                }
                            })


//                      var iframeWindow = window['layui-layer-iframe'+ index]
//                          ,submitID = 'LAY-user-back-submit'
//                          ,submit = layero.find('iframe').contents().find('#'+ submitID);
//                      //监听提交
//                      iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
//                          var field = data.field; //获取提交的字段
//                          alert("ok");
//                          console.log(field);
//
//
//                          table.reload('LAY-user-front-submit'); //数据刷新
//                          layer.close(index); //关闭弹层
//                      });

//                      submit.trigger('click');
                        }
                    });
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });


    })
    ;
</script>

</body>
</html>

